package com.shop.service;

import cn.hutool.core.bean.BeanUtil;
import com.shop.dao.AddressMapper;
import com.shop.dto.*;
import com.shop.entity.AddressEntity;
import com.shop.local.UserInfoLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wzby
 * @date 2022-12-02 13:24:15
 */
@Service
public class AddressService {
    @Autowired
    private AddressMapper addressMapper;

    public Integer insert(AddressDto addressDto) {
        AddressEntity addressEntity = BeanUtil.copyProperties(addressDto, AddressEntity.class);
        UserInfo userInfo = UserInfoLocal.get();
        addressEntity.setMemberId(userInfo.getMemberId());
        if (addressDto.getIsDefault()) {
            addressMapper.updateDefaultFlag(userInfo.getMemberId());
        }
        return addressMapper.insert(addressEntity);
    }

    public List<AddressDto> select(AddressQueryDto addressQueryDto){
        UserInfo userInfo = UserInfoLocal.get();
        addressQueryDto.setMemberId(userInfo.getMemberId());
        List<AddressEntity> addressEntities = addressMapper.select(addressQueryDto);
        return BeanUtil.copyToList(addressEntities, AddressDto.class);
    }
}
